package com.wsx.ones.boot.core.redis;

import java.util.List;
import java.util.zip.CRC32;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;

public class RedisUtil {
	
	//jedisGroup组选择
	public static final String CONT_CLUSTERNAME_SESSION = "session";
	public static final String CONT_CLUSTERNAME_ROUTE = "route";
	public static final String CONT_CLUSTERNAME_PUBLICDATA = "publicData";
	
	
	/**
	 * 
	 * @param key
	 * @param group
	 * @return
	 */
	private static Jedis getJedis(String key, String group) {
		Jedis jedis = null;
		try {
			if (null == RedisConfig.redisGroup) {
				RedisConfig.redisGroup = RedisConfig.getRedisGroup();
			}
			List<RedisBean> redisBeans = RedisConfig.redisGroup.get(group);
			int redisSize = redisBeans.size();
			
			int redisNo = getRedisNo(key, redisSize);
			RedisBean redisBean = redisBeans.get(redisNo);
			
			jedis = new Jedis(redisBean.getHostIp(), Integer.valueOf(redisBean.getPort()));
			jedis.select(Integer.valueOf(redisBean.getSelectdb()));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return jedis;
	}
	
	/**
	 * 
	 * @param key
	 * @param redisSize
	 * @return
	 */
	private static int getRedisNo(String key, int redisSize) {
		long hashValue = hash(key);
		return (int) (hashValue % redisSize);
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	private static long hash(String key) {
		CRC32 crc32 = new CRC32();
		crc32.update(key.getBytes());
		return crc32.getValue();
	}
	
	private static void closeJedis(Jedis jedis) {
		try {
			if (jedis.isConnected()) {
				jedis.quit();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			if (jedis.isConnected()) {
				jedis.disconnect();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void insertObject(String key, Object object, String group) {
		Jedis jedis = null;
		try {
			jedis = getJedis(key, group);
			Gson gson = new Gson();
			jedis.set(key, gson.toJson(object));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				closeJedis(jedis);
			}
		}
	}

	public static void deleteObject(String key, String group) {
		Jedis jedis = null;
		try {
			jedis = getJedis(key, group);
			jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				closeJedis(jedis);
			}
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object getObject(String key, Class target, String group) {
		Jedis jedis = null;
		Object object = null;
		try {
			jedis = getJedis(key, group);
			String sObj = jedis.get(key);
			if (null != sObj && !"{}".equals(sObj)) {
				Gson gson = new Gson();
				object = gson.fromJson(sObj, target);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != jedis) {
				closeJedis(jedis);
			}
		}
		return object;
	}
	
	public static void insertObject(String key, Object object, String group, int expireSeconds) {
		Jedis jedis = null;
		try {
			jedis = getJedis(key, group);
			Gson gson = new Gson();
			jedis.setex(key, expireSeconds, gson.toJson(object));
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != jedis) {
				closeJedis(jedis);
			}
		}
	}
	
	public static void resetExpire(String key, String group, int expireSeconds) {
		Jedis jedis = null;
		try {
			jedis = getJedis(key, group);
			jedis.expire(key, expireSeconds);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != jedis) {
				closeJedis(jedis);
			}
		}
	}
}
